package com.app.controller.supermarket;
import com.app.dto.supermarket.MyOddDto;
import com.app.mapper.supermarket.*;
import com.app.model.supermarket.*;
import com.app.service.supermarket.MktPersonService;
import com.app.service.supermarket.MyOddService;
import com.app.service.supermarket.OddJobReleaseService;
import com.app.service.supermarket.SkillSonService;
import com.app.util.*;
import com.app.vo.supermarket.MktPersonVo;
import com.app.vo.supermarket.MyOddVo;
import com.app.vo.supermarket.OddJobReleaseReturnVo;
import com.app.vo.supermarket.OddJobReleaseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Controller
@RestController
public class MyOddController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private MyOddService mapper;
    @Autowired
    private MyOddService myOddService;
    @Autowired
    private MktPersonService mktPersonService;
    @Autowired
    private OddJobReleaseService oddJobReleaseService;

    private OddJobReleaseMapper oddJobReleaseMapper;
    @Autowired
    private SkillSonService skillSonService;
    @Autowired
    MktPersonRepository mktPersonRepository;
    @Autowired
    MyOddMapper myOddMapper;

    //增
    @GetMapping("/insertMyOdd")
    public Result<Object> insertMyOdd(@RequestParam("oid") Integer oid,
                              HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        MyOddModel myOddModel = mapper.getModelByMidAndOid(mktPerson.getId(),oid);


        if(myOddModel != null ){
            myOddModel.setState(1);
            mapper.update(myOddModel);
        }else {
            myOddModel.setOid(oid);
            myOddModel.setMid(mktPerson.getId());
            myOddModel.setState(1);
            myOddModel.setCrtTime(CommonHelp.getNowTimestamp());
        mapper.insert(myOddModel);
        }

        return Result.success("操作成功");
    }


    //修改状态
    @GetMapping("/upMyOddState")
    public Result<Object> upMyOddState(HttpServletRequest request) throws ParseException {
        String oId = request.getParameter("oId");
        String state = request.getParameter("state");
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        String sql = "update tb_my_odd set state= '" + state + "'  where  oid='" + oId + "'  AND mid='" + mktPerson.getId() + "' ";
        jdbcTemplate.update(sql);

        return Result.success("操作成功");
    }

    @GetMapping("/getMyOddList")
    public Result<Object> getMyOddList(
            @RequestParam("state") Integer state,
            HttpServletRequest request) throws ParseException {

        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        List<MyOddModel> modelList = mapper.getOddByMid(mktPerson.getId(), state);
        for (int i = 0; i < modelList.size(); i++) {
            OddJobReleaseModel oddList = modelList.get(i).oddLIst;
            Timestamp data1 = Timestamp.valueOf(oddList.getStartTime());//开始时间
            Timestamp data2 = Timestamp.valueOf(oddList.getEndTime());//开始时间
            String strn = new SimpleDateFormat("yyyy-MM-dd ").format(data1);
            String strn1 = new SimpleDateFormat("yyyy-MM-dd ").format(data2);

            oddList.setStartTime(strn);
            oddList.setEndTime(strn1);
        }
        for (int i = 0; i < modelList.size(); i++) {
            //1已申请2待结算3已完工4待评价5完成6取消
            if (modelList.get(i).getState() == 1) {
                modelList.get(i).setStateName("已申请");
            }
            if (modelList.get(i).getState() == 2) {
                modelList.get(i).setStateName("待开工");
            }
            if (modelList.get(i).getState() == 3) {
                modelList.get(i).setStateName("已完工");
            }
            if (modelList.get(i).getState() == 4) {
                modelList.get(i).setStateName("待评价");
            }
            if (modelList.get(i).getState() == 5) {
                modelList.get(i).setStateName("已完成");
            }
            if (modelList.get(i).getState() == 6) {
                modelList.get(i).setStateName("已取消");
            }
        }
        int counts = myOddService.countMid(mktPerson.getId());
        MyOddVo vo = new MyOddVo();
        vo.setList(modelList);
        vo.setCount(counts);
        return Result.success(vo);
    }

    @GetMapping("/getMyOddMakList")
    public Result<Object> getMyOddMakList(HttpServletRequest request) throws ParseException {
        String oid = request.getParameter("oid");
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel customer = mktPersonService.getModelByMobile(mobile);
        if(customer==null)
        {
            return Result.error(405,"用户不存在");
        }
        MyOddModel model = new MyOddModel();
        model.setOid(Integer.valueOf(oid));
        List<MyOddModel> modelList = mapper.getMyOddMak(model);
        if (modelList != null && modelList.size() > 0) {
            for (int i = 0; i < modelList.size(); i++) {
                MktPersonModel mktPerson = mktPersonService.getPersonByMid(modelList.get(i).getMid());
                if(mktPerson.getSkills() == null && mktPerson.getSkills() == "" || mktPerson.getSkills().length() <= 0){
                    Result.error(500,"暂无标签");
                }else {String skillsString = mktPerson.getSkills().substring(0, mktPerson.getSkills().length()-0 );
                    String[] result = skillsString.split(",");
                    List<SkillSonModel> skillSonList = new ArrayList<>();
                    for (int j = 0; j < result.length; j++) {
                        SkillSonModel son  = skillSonService.getSkillSon(Integer.parseInt(result[j]));
                        skillSonList.add(son);
                    }
                    modelList.get(i).setSkillSonList(skillSonList);}

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(modelList.get(i).getMkt().getBirthday() + "-01").getTime());
                String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                modelList.get(i).getMkt().setAge(date);
                MktPersonVo mkt = modelList.get(i).mkt;
                String birthday = sdf.format(mkt.getBirthday());
                String creatTime = sdf.format(mkt.getCrtTime());
                mkt.setBir(birthday);
                mkt.setCreatTime(creatTime);

                if (modelList.get(i).getMkt().getSex() == 1) {
                    modelList.get(i).getMkt().setSexText("男");
                } else {
                    modelList.get(i).getMkt().setSexText("女");
                }
            }

        }
        return Result.success(modelList);
    }

    @GetMapping("/getMyOddMakIsExistence")
    public Result<Object> getMyOddMakIsExistence(HttpServletRequest request) throws ParseException {
        String oid = request.getParameter("oid");
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        MyOddModel model = new MyOddModel();
        model.setOid(Integer.valueOf(oid));
        List<MyOddModel> modelList = mapper.getModelByPid(mktPerson.getId(), Integer.valueOf(oid));

        int OddCount = mapper.getCountByOidAndState(Integer.valueOf(oid));
        int OddJobCount = oddJobReleaseService.selectNumber(Integer.valueOf(oid));
        OddJobReleaseReturnVo vo = new OddJobReleaseReturnVo();
        if(OddCount == OddJobCount){
            vo.setJobState(8);
        }else {
            vo.setJobState(9);
        }
            //检查逻辑 1抢活2未抢活3录用3活录取满了
            if (modelList != null && modelList.size() > 0) {
                if (modelList.get(0).getState() > 1 && modelList.get(0).getState() < 6) {
                    vo.setType(3);
                } else if (modelList.get(0).getState() == 6) {
                    vo.setType(2);
                } else {
                    vo.setType(1);
                }
            } else {
                vo.setType(2);
            }
        return Result.success(vo);
    }
    @ApiOperation(value = "录用")
    @RequestMapping(value = "/update")
    public Result<Object> updState(@RequestParam Integer id, @RequestParam Integer mid,HttpServletRequest request) {
        if (id == null || mid == null) {
            Result.error(500,"id是零工id不能为空！");
        }
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        QueryWrapper<MyOddDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", id);
        MktPersonQueryParamModel mktPersonVo = mktPersonRepository.selectById(mid);
        queryWrapper.eq("mid", mktPersonVo.getId());
        queryWrapper.eq("state", 1);
        List<MyOddDto> myOdds = myOddMapper.selectList(queryWrapper);
        MyOddDto myOdd = new MyOddDto();
        myOdd.setId(myOdds.get(0).id);
        myOdd.setOid(id);
        myOdd.setMid(mktPersonVo.getId());
        myOdd.setState(2);
        myOddMapper.updateById(myOdd);

        return Result.success("操作成功");
    }

    @ApiOperation(value = "申请人数")
    @GetMapping("/getNumber")
    public Result<Object> getNumber(HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        int count = myOddMapper.getCountByMid(mktPerson.getId());
        return Result.success(count);
    }

}
